Okay.
So today we are taking a look at messaging service.
Now this is um, I think one of the the only one, if not a couple topics that you kind of have to trust
me on.
Um, the reason why is because it doesn't work in studio.
Um, and you might understand why, if I explain it to you first.
So messaging service allows you to send messages between servers.
So this is extremely useful because it allows you to sync up information between service servers.
So um, things like a leaderboard for example.
Um, a leaderboard would be a global leaderboard.
So no matter what server each player is on, they should see the same leaderboard, the same values
and all the same stuff.
Um, live events as well.
If you want a live event to occur on all servers at the same time, then you absolutely need to do it
that way.
You can't say, for example, um, I don't know, um, when the server starts, wait, how many seconds?
And then do below events because different servers active for different lengths of time.
So it just wouldn't work.
Um, I was gonna think of another use there.
Oh, yes.
If you're wanting to play, to be able to join different servers from the game like a guy comes up vs
the active servers, your friends are on this server, do you want to join them?
Stuff like that.
You need to sync it up between servers.
Now, the reason why you have to trust me on this one is because, um, when you are testing the game
in the studio, it only launches one server and there's no way to launch two servers at once in studio.
So there's no way I can show you this in the studio.
Um, so I'd highly recommend you, um, writing some code and trying publishing your game and trying
it out, playing it through Roblox.
But unfortunately, just something I can't show you.
And I think there are a couple of examples of stuff that is not possible to show you because of what
the scripting involves.
Um, but I promise you, I've tested this code and I know that it works.
Okay, so, um, messaging service has two parts now, um, these two parts will be in different servers.
Um, now, obviously I can't do that.
So I'm going to just make two different server scripts.
But I've just got to imagine that they're in two different servers.
Okay.
So let's go to Server Script Service and add this here okay.
So you can think of messaging service.
Um, kind of like YouTube.
Um I believe everyone knows YouTube.
So hopefully that's a good example to give.
But um, there's two parts to it.
There's subscribing and there's publishing.
So um, the player so sorry, the, um, you will subscribe to a certain topic and when, when you publish
a message to that topic, all servers which are listening to that topic then receive it is basically
how it works.
It's kind of like a YouTube notification system, but for messages.
Um, so let's have a look first at the subscribing bit first, as you first need to subscribe to the
topic before you then receive, um, receive things back from that topic.
So you need to make sure that subscribing is the first thing you need to do.
So we're going to fetch messaging service.
So um, local messaging.
Service is equal to game.
Get service messaging service like that there.
Okay.
Now, none of this code is really too difficult.
There is a handy function called subscribe async which subscribes this script to a certain topic.
So messaging service colon subscribe async like that there.
And this gives you the topic list it as a string.
So this can be whatever you'd like.
It can be any any any string.
As long as you make sure you use the same one for all the different scripts.
And so I'm going to just call this announcements.
But of course, depending on what you want to do, give it a different name.
Just like with variables.
Give it a name that's relevant to what's going on, basically, and you then write a callback.
And this is where we use slightly different syntax.
So this this callback is basically a function which should be fired when this message is received.
So once they get once when you receive a notification from this topic, it should then run this function.
So we don't have a function.
All we need to use.
So let's create one.
So let's write the keyword function pair of brackets and press enter outside the brackets.
And then made this into a function.
Now this bit here is not necessarily a function or this bit here is.
But this bit and this bit aren't function aren't functions.
But this here we're creating a new function where we can write any code in.
Might look a bit strange at first, but it would be.
It would be the same as creating it before.
And then putting it in here, I just think is a bit quicker.
And even though a different way might be easier for people to read if, if, if a different way like
this is quicker, it is good, you know, knowing about it and nothing will stop you from watching a
tutorial on YouTube.
Um, I don't know.
You say you wanted to learn something very specific and you were stuck on making leaderboard in the
future, and you might search up how to do it either on YouTube or anything else, and you realize that
they've done it a different method.
No idea what it is.
It's just sometimes those different methods can be the methods that make your code easier to read.
Okay, anyway, enough of that.
So there is a parameter in here we need to write, which is the message this contains.
Um, some different things to do with the message that was sent over.
Okay.
So some things you can reference with a message we have message dot.
Um I won't show you okay.
It shows you a couple of things.
Um, the most important is probably message dot data.
This is whatever the message was sent.
So whatever was sent from the message.
So let's print message dot data like that for example.
Um, also another one you can write down is message dot sent.
And this is when the, um, the time at which the message, the message was sent.
So if you are looking at a leaderboard, for example, not not, not the one built in, but an actual
physical leaderboard in your game, um, the publisher script would be controlling all the things such
as, um, when a player in that.
Um, yeah, if a player is on the leaderboard and if they change their coins value or, um, player
75 on leaderboard overtakes 70% player 70 for the send over that as the data.
So um, the send over things like okay, this player here is now on this place of leaderboard.
So you can send stuff over to, to control all that kind of stuff.
Really.
Um, leaderboards.
The most difficult bit about making a leaderboard is all of the sinking up and stuff like that.
Um, but that's it from this script here.
So it's whatever code you want to occur when the message, um, is won.
Now, there's no point me giving you any examples on this or writing out some script which, um, allows
you to add parts in or any kind of quick example for me to show you because it won't work anyway.
So you just trust me on this one.
But, um, it would work if you try it out properly.
Um, okay.
So this is your subscribing section here.
Let's then look at how we can publish a message in into this topic.
So let's add another script in and same variable messaging service.
I could have copied and pasted it, but um, won't take me too long to write out.
Okay.
And I'm going to put a wait for about five seconds.
The reason why is because if I, um, publish here quickly first and then do this as well, it takes
less time to, um, read the single line of code we're going to write, which publishes a message into
the topic, than it does to for the script to understand or to read what it needs to do when a um,
oh yeah.
So to script to be able to read it, it needs to subscribe and understand what it needs to do.
Um, when a message is sent.
So what actually happens is that they, a pub message is published and a couple of milliseconds later
it then subscribes to it.
So it's like someone a YouTuber posts a video at 8:00 and 801.
You subscribe to their channel, you're going to get a notification for that.
So is what that Wayfair is for.
But obviously in a less kind of temporary setup, you would obviously want to replace that with something
else and obviously be happening with certain events occurring and all that kind of stuff.
Anyway, um, this one is just messaging service colon publish async.
So on.
Subscribe async once publish async.
Not too difficult to remember the topic which makes sure it's spelt correctly.
Announcements if you spell it wrong or spell it differently to what it is in your other scripts, it
will not be the correct topic so you will not receive the message.
Um, and then we can pass over any data.
So this can be whatever you'd like.
Any function, any boolean, anything you like.
Um, but let's just pass over a string called data like that there.
Okay.
And, um, let's just try this out then I think maybe it would work, but obviously not in the way that's
first intended, I believe.
Um, okay.
So yeah, it does send that over so it while it does send while you can physically see it working,
it's just it's between two server scripts, which is not it's, which is not its use basically.
Um, so while you can see it working, its proper use is you can't really see.
Um, so there's no like, yeah, this is what you can do with it.
Really cool.
It's just this is the code for it.
And I promise you it does work.
Um, you just have to believe me a bit there.
Um, but.
Yeah.
Um, is there anything else mentioned about this?
Um, this number here is, is the time that it was sent, and it's a long number.
And if you're not too familiar with coding and things like that, um, you're probably very confused
as to why is the time an extremely long number?
Well, I'm not going to get into that.
The reason why is because there was a whole lecture on this area.
And but if you're interested now and you want to you want to you want to know why that is the case.
Go watch my lecture later on in this section about time.
And it will explain that completely.
Or just wait and and watch the rest of them.
And then when you get once you do get to the time lecture, um, it'll be explained for you there.
There's no point explaining it now and explaining it again later on.
Um, but yeah.
Okay.
Cool.
So hopefully that's, um, been interesting for you and being able to unlock a couple extra things you've
been able to do in your game.
Okay.
So hope you've enjoyed that and and join me in the next lesson, next, next lesson where we continue
to look at some more topics.